package com.project.card.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.project.card.MainActivity;
import com.project.card.OrderActivity;
import com.project.card.R;
import com.project.card.bean.GoodBean;

import java.util.ArrayList;

public class GoodAdapter extends ArrayAdapter<GoodBean> {
    private Context context;
    private int mresource;
    
    private Dialog dialog;
    
    
    private ArrayList<GoodBean> objects;

    
    public GoodAdapter(@NonNull Context context, int resource, @NonNull ArrayList<GoodBean> objects) {
        super(context, resource, objects);
        this.context=context;
        this.mresource=resource;
        this.objects = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        convertView= LayoutInflater.from(this.context).inflate(this.mresource,parent,false);
        ImageView image=convertView.findViewById(R.id.good_img);
        TextView title=convertView.findViewById(R.id.good_title);
        TextView price=convertView.findViewById(R.id.good_price);
        
        

        image.setImageResource(getItem(position).getImage());
        title.setText(getItem(position).getTitle());
        price.setText(String.valueOf(getItem(position).getPrice()));
        //return super.getView(position, convertView, parent);
        final View view = convertView;

        convertView.setOnClickListener(f ->{
            SharedPreferences sp = context.getSharedPreferences("current_user",Context.MODE_PRIVATE);
            boolean isLogin = sp.getBoolean("login",false);
            if(isLogin){
                Intent intent = new Intent(getContext(), OrderActivity.class);
                intent.putExtra("title",getItem(position).getTitle());
                intent.putExtra("id",position+1);
                intent.putExtra("price",getItem(position).getPrice());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }else {
                
            }
        });
        
        
        return convertView;
    }
}
